package admin

import (
	"duoke/model"
	"duoke/service"
	"duoke/utils/gconv"
	"duoke/utils/gtime"
	"duoke/utils/orm"
	"duoke/utils/response"
	"github.com/gin-gonic/gin"
)

var Reward = new(rewardCtl)

type rewardCtl struct{}

func (ctl *rewardCtl) List(c *gin.Context) {
	var req model.ReqList
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	where := "id > 0 "
	if req.Keyword != "" {
		where += "AND (user_id ='" + req.Keyword + "' OR from_id ='" + req.Keyword + "' OR nickname ='" + req.Keyword + "' OR remark like '%" + req.Keyword + "%' )"
	}
	if req.State != "" {
		where += "AND state = '" + req.State + "' "
	}
	if req.Type != "" {
		where += "AND type = '" + req.Type + "' "
	}
	if req.GoodsId != "" {
		where += "AND goods_id = '" + req.GoodsId + "' "
	}
	if req.TimeRange != nil && len(req.TimeRange) == 2 {
		startTime := gtime.StrToTime(req.TimeRange[0])
		endTime := gtime.StrToTime(req.TimeRange[1])
		where += "AND created_at >= '" + gconv.String(startTime) + "' "
		where += "AND created_at <= '" + gconv.String(endTime) + "' "
		// fmt.Println(where)
	}
	list, total, totalAmount, err := service.Reward.Page(c, req, where, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	response.Success(c, "", gin.H{"total": total, "list": list, "totalAmount": totalAmount})
}
func (ctl *rewardCtl) Send(c *gin.Context) {
	var req model.ReqId
	if err := c.ShouldBind(&req); err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	reward, err := service.Reward.GetById(req.Id, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if reward.State != 0 {
		response.Error(c, "该奖励已发放", nil)
		return
	}
	// 使用退款渠道发放
	goods, err := service.Goods.GetById(reward.GoodsId, "")
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	if gconv.Int(goods.Config["is_send_refund_redbag"]) == 1 {
		where := "goods_id =" + gconv.String(reward.GoodsId) + " "
		where += "AND user_id =" + gconv.String(reward.UserId) + " "
		where += "AND pay_state = 1 "
		where += "AND refund_state = 0 "
		order, err := service.Order.GetByWhere(where, "")
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		if order.Id == 0 {
			response.Error(c, "发放人没有已支付订单记录，无法退款发放奖励", nil)
			return
		}
		if gconv.Float64(reward.Amount) > gconv.Float64(order.PayAmount) {
			response.Error(c, "奖励金额大于订单支付金额", nil)
			return
		}
		isSuccess, err := service.WechatPay.RewardRefund(c, gconv.Float64(reward.Amount), gconv.Float64(order.PayAmount), reward.OrderNo)
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		if isSuccess {
			response.Success(c, "发放成功", nil)
		}
		response.Error(c, "发放失败", nil)
	}
	// 商转零发放
	isSuccess, err := service.WechatPay.TransferV3(c, reward.Openid, gconv.Float64(reward.Amount), reward.OrderNo, reward.Remark)
	if err != nil {
		reward.FailMsg = err.Error()
		_, _ = orm.Db.ID(reward.Id).Update(reward)
		response.Error(c, err.Error(), nil)
		return
	}
	if isSuccess {
		reward.State = 1
		_, _ = orm.Db.ID(reward.Id).Update(reward)
		response.Success(c, "发放成功", nil)
		return
	} else {
		reward.FailMsg = "零钱发放失败"
		_, _ = orm.Db.ID(reward.Id).Update(reward)
		response.Error(c, "发放失败", nil)
		return
	}
}
